﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq.Expressions;
using Newtonsoft.Json.Linq;
using WebGrease.Css.Extensions;

using CanDoo;
using CanDoo.Core;
using CanDoo.Data;
using CanDoo.Web;
using CanDoo.FineUI;
using CanDoo.Extensions;
using CanDoo.Oil.Core.Models;
using CanDoo.Oil.Core.Contracts;
using CanDoo.Admin.Core.Contracts;
using CanDoo.Core.Data;
using FineUIPro;

namespace CanDoo.Oil.WebApp.storeStock
{
    public partial class Default : ListPageBase<IStockContract, Stock>
    {


        public IWarehouseContract WarehouseContract { get; set; }
        public IProductContract ProductContract { get; set; }

        #region 方法
        protected override void InitGrid()
        {
            Selector = p => new
            {
                p.Warehouse,
                p.WarehouseId,
                p.Product,
                p.ProductId,
                p.HighPrice,
                p.LowPrice,
                p.NowPrice,
                p.CostPrice,
                p.StockNum,
                p.OnWayNum,
                p.WaitBackNum,
                p.ReasonableNum,
                p.TopNum,
                p.BottomNum,
                p.EnableAlerm,
                p.Remark,
                p.Id,
                p.Name
            };
            base.InitGrid();
        }

        protected override void Search()
        {
            var key = SearchKey.GetValue();


            var warehouseId = Web.Request.QueryString("warehouseId").ToLong();
            var productId = Web.Request.QueryString("productId").ToLong();
            if (warehouseId > 0L)
            {
                SearchWarehouse.Readonly = true;
                SearchWarehouse.SetValue(warehouseId);
            }
            else
                warehouseId = SearchWarehouse.GetValue().ToLong();

            if (productId > 0L)
            {
                SearchProduct.Readonly = true;
                SearchProduct.SetValue(productId);
            }
            else
                productId = SearchProduct.GetValue().ToLong();

            if (string.IsNullOrEmpty(key) == false)
            {
                Filter = Filter.And(p => p.Product.Name.Contains(key));
            }
            if (warehouseId >  0L)
                Filter = Filter.And(p => p.WarehouseId == warehouseId);
            if (productId >  0L)
                Filter = Filter.And(p => p.ProductId == productId);
        }
        #endregion

        #region 事件
        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //FineUIListHelper.BindDDL<Warehouse>("仓库",WarehouseContract.GetResolvedTree(), 0L, SearchWarehouse);
                FineUIListHelper.BindDDL(WarehouseContract.EntityName, this.WarehouseContract.GetSelectList(), 0L, SearchWarehouse);
                //FineUIListHelper.BindDDL<Product>("产品",ProductContract.GetResolvedTree(), 0L, SearchProduct);
                FineUIListHelper.BindDDL(ProductContract.EntityName, this.ProductContract.GetSelectList(), 0L, SearchProduct);
                InitGrid();
            }
        }
        #endregion

        #endregion 事件

    }
}
